<template>
  <!-- 借还记录 -->
  <div id="app" class="SkCirculateModel">
    <j-modal
      :title="title"
      :width="width"
      :visible="visible"
      switchFullscreen
      @ok="handleOk"
      :confirmLoading="confirmLoading"
      :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
      @cancel="handleCancel"
      cancelText="关闭"
    >
      <a-table
        v-if="modelFlag"
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
      </a-table>
      <a-spin :spinning="confirmLoading" v-else>
        <j-form-container>
          <a-form-model ref="form" :model="model"  slot="detail" :rules="validatorRules">
            <a-row>
              <a-col :span="24">
                <a-form-model-item label="归还时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="returnTime">
                  <a-date-picker
                    showTime
                    value-format="YYYY-MM-DD HH:mm:ss"
                    format="YYYY-MM-DD HH:mm:ss"
                    placeholder="请选择实际归还时间"
                    v-model="model.returnTime"
                    :disabledDate="disabledBeforeDate"
                    style="width:100%;"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="归还数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="returnNum">
                  <a-input-number v-model="model.returnNum" placeholder="请输入归还数量" style="width: 100%" />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="归还人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="returnUser">
                  <a-input v-model="model.returnUser" placeholder="请输入归还人" style="width: 100%" />
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model>
        </j-form-container>
      </a-spin>
    </j-modal>
  </div>
</template>
<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import moment from 'moment'
import { httpAction, getAction } from '@/api/manage'
export default {
  name: 'SkCirculateModel',
  mixins:[JeecgListMixin],
  components:{
  },
  data() {
    return {
      title: '归还记录',
      width: 600,
      visible: false,
      disableSubmit: false,
      // 表头
      columns: [
        {
          title: '归还时间',
          align: 'center',
          dataIndex: 'returnTime'
        },

        {
          title: '归还数量',
          align: 'center',
          dataIndex: 'returnNum'
        },
        {
          title: '归还人',
          align: 'center',
          dataIndex: 'returnUser'
        }
      ],
      model:{},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading:false,
      url: {
        add:"/management/skToolReturnRecord/add",
        list: "/management/skToolReturnRecord/list",
        delete: "/management/skToolLendRecord/delete",
      },
      modelFlag:null,
      lendNum:0,
      validatorRules: {
        returnNum: [
          { required: true, message: '请输入归还数量!' },
          {validator: this.validateCanLentNum}
        ],
        returnUser: [
          { required: true, message: '请输入归还人!' }
        ],
        returnTime: [
          { required: true, message: '请选择归还时间!' }
        ],
      },
    }
  },

  filters: {},

  computed: {},

  created() {},

  activated() {},

  mounted() {},

  methods: {
    // 禁用今天之前的日期
    disabledBeforeDate(current) {
      return current && current > moment().endOf('day')
    },
    init(record,flag) {
      this.model = {}
      this.model.toolLendId = record.id;
      this.lendNum = record.unReturnNum;
      this.queryParam.toolLendId = record.id;
      this.loadData();
      this.modelFlag = flag;
      this.visible = true
    },
    // 可借数量判断
    validateCanLentNum(rule, value, callback) {
      if (value <= this.lendNum && value > 0 ) {
        callback();
      } else {
        callback("当前教具未归还数量为" + this.lendNum);
      }
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      console.log(this.model)
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (this.modelFlag) {
            httpurl += this.url.edit
            method = 'put'
          } else {
            httpurl += this.url.add
            method = 'post'
          }
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.close();
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    submitCallback() {
      this.$emit('ok')
      this.visible = false
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>
<style scoped>
/* @import url(); 引入css类 */
</style>
